package com.seewo.dubbo.debugger.admin.controller;

import com.alibaba.fastjson.JSON;
import com.seewo.dubbo.debugger.admin.common.validate.group.Query;
import com.seewo.dubbo.debugger.admin.entity.Interface;
import com.seewo.dubbo.debugger.admin.entity.Method;
import com.seewo.dubbo.debugger.admin.entity.Service;
import com.seewo.dubbo.debugger.admin.service.IInterfaceService;
import com.seewo.dubbo.debugger.admin.service.IMethodService;
import com.seewo.dubbo.debugger.admin.service.IServiceService;
import com.seewo.dubbo.debugger.admin.utils.CommonUtil;
import com.seewo.dubbo.debugger.api.model.ModelDef;
import com.seewo.dubbo.debugger.api.model.ParamDef;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 前端页面控制器
 *
 * @author chenliangliang
 * @date 2018/8/3
 */
@Controller
@RequestMapping("/page")
public class PageController {

    @Autowired
    private IServiceService serviceService;

    @Autowired
    private IInterfaceService interfaceService;

    @Autowired
    private IMethodService methodService;

    @GetMapping("/display")
    public String display() {
        return "display";
    }


    @GetMapping("/interface/list")
    public String listInterface(@Validated(Query.class) Service service, Model model) {
        Service serviceInfo = serviceService.getInterfaceList(service);
        List<Interface> interfaces = serviceInfo.getInterfaces();
        model.addAttribute("protocol", serviceInfo.getCProtocol());
        model.addAttribute("address", serviceInfo.getCAddress());
        model.addAttribute("serviceId", serviceInfo.getCId());
        model.addAttribute("serviceName", serviceInfo.getCName());
        model.addAttribute("serviceDesc", serviceInfo.getCDesc());
        model.addAttribute("interfaces", interfaces);
        return "interface-list";
    }

    @GetMapping("/method/list")
    public String listMethod(@Validated({Query.class}) Interface anterface, Model model) {
        Interface anInterface = interfaceService.getMethods(anterface);
        List<Method> methods = anInterface.getMethods();
        model.addAttribute("serviceId",anInterface.getCServiceId());
        model.addAttribute("methods", methods);
        model.addAttribute("name", anInterface.getCName());
        model.addAttribute("desc", anInterface.getCDesc());
        model.addAttribute("interfaceId", anInterface.getCId());
        return "debug";
    }


    @GetMapping("/api")
    public String api(@Validated(Query.class) Method method, Model model) {
        try {
            Method api = methodService.getApi(method);
            model.addAttribute("methodDesc", api.getCDesc());
            model.addAttribute("methodName", api.getCName());
            List<ParamDef> requestParams = api.getRequestParams();
            for (ParamDef paramDef : requestParams) {
                ModelDef modelDef = paramDef.getModelDef();
                if (modelDef != null) {
                    paramDef.setModelJson(JSON.toJSONString(modelDef, true));
                    String requestJson = CommonUtil.parse2RequestJson(modelDef.getPropertyList());
                    paramDef.setRequestJson(CommonUtil.formatJson(requestJson));
                }
            }
            model.addAttribute("requestParams", requestParams);

            ParamDef responseParam = api.getResponseParam();
            ModelDef modelDef = responseParam.getModelDef();
            if (modelDef != null) {
                responseParam.setModelJson(JSON.toJSONString(modelDef, true));
            }
            model.addAttribute("responseParam", responseParam);
            model.addAttribute("methodId", api.getCId());
            return "api";
        } catch (Exception e) {
            model.addAttribute("msg", "当前方法不可调试，请引入相应依赖");
            return "error-page";
        }
    }




}
